package cn.itsource.basic.utils;

import cn.itsource.basic.jwt.JwtUtil;
import cn.itsource.basic.jwt.Payload;
import cn.itsource.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取当前登录人信息
 */
public class LoginContextUtils {

    public static Logininfo getLogininfo(HttpServletRequest request){
        String jwtToken = request.getHeader("token");
        if(jwtToken!=null){
            try {
//                PublicKey publicKey = RsaUtils.getPublicKey(JwtUtil.class.getClassLoader().getResource("auth_rsa.pub").getFile());
//                Payload<Logininfo> payload = JwtUtil.parseJwtToken(jwtToken, publicKey, Logininfo.class);
//                return payload.getLoginData().getLogininfo();
                Payload payload = JwtUtil.parseJwtToken(jwtToken);  //解密
                return payload.getLogininfo();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }
}